package com.example.company.controller;


import com.example.company.entity.ApiResult;
import com.example.company.entity.ApiResultHandler;
import com.example.company.entity.Leaves;
import com.example.company.entity.Positions;
import com.example.company.mapper.LeavesMapper;
import com.example.company.service.ILeavesService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 存储员工请假信息 前端控制器
 * </p>
 *
 * @author 88Ra
 * @since 2024-12-31
 */
@CrossOrigin
@RestController
@RequestMapping("/api/leaves")
public class LeavesController {

	@Autowired
	private LeavesMapper leavesMapper;
	
	@Autowired
	private ILeavesService leavesService;
	
	
	@PostMapping("/insert")
	public ApiResult insertLeaves(@RequestBody Leaves leaves){
		System.out.println(leaves);
		System.out.println("Department ID: " + leaves.getDepartmentid());
		int cur= leavesService.insertLeaves(leaves);
		if (cur>= 1) {
			return ApiResultHandler.buildApiResult(200, "请求成功", leaves);
		} else {
			return ApiResultHandler.buildApiResult(400, "请求失败", null);
			
		}
		
	}
	@GetMapping("/list")
	public PageInfo<Leaves> findPage(@RequestParam(defaultValue = "1") Integer pageNum
			, @RequestParam(defaultValue = "10") Integer pageSize, Leaves leaves) {
		
		PageInfo<Leaves> pageInfo=leavesService.findPage(pageNum, pageSize, leaves);
		return pageInfo;
	}
	@CrossOrigin
	@GetMapping("/update/{leaveid}")
	public ApiResult updata(@PathVariable("leaveid") Integer leaveid){
		int cur =leavesMapper.updateByleaveid(leaveid);
		if (cur>= 1) {
			return ApiResultHandler.buildApiResult(200, "请求成功", cur);
		} else {
			return ApiResultHandler.buildApiResult(400, "请求失败", null);
			
		}
	}
	
	@CrossOrigin
	@GetMapping("/updatetono/{leaveid}")
	public ApiResult updatetono(@PathVariable("leaveid") Integer leaveid){
		int cur =leavesMapper.updateByleaveidtono(leaveid);
		if (cur>= 1) {
			return ApiResultHandler.buildApiResult(200, "请求成功", cur);
		} else {
			return ApiResultHandler.buildApiResult(400, "请求失败", null);
			
		}
	}
	
	
}
